<template>
  <q-page>
    <!-- 现在这里打算展示所有归属于当前用户的赛事列表，因为后期还有个人信息的维护，所以专门留出一个页面 -->
    <div class="">
    <div class="">
      <q-card flat>
        <q-tabs
          v-model="tab"
          class="bg-primary text-white shadow-2"
          narrow-indicator
          flat
        >
          <q-tab name="comps" icon="filter_1" label="我的赛事" />
          <q-tab name="info-manage" icon="sentiment_very_satisfied" label="账户管理" />
        </q-tabs>

        <q-separator />

        <q-tab-panels v-model="tab" animated>
          <q-tab-panel name="comps">
            <div class="q-px-lg q-pb-md">
              <q-timeline :layout="layout" color="secondary">
                <template v-for="(item, idx) in getMyList" :key="idx">
                  <q-timeline-entry
                    :title="(item.cate_name)+(item.cate.collect_part === 'name'?'-'+item.name:'')"
                    :subtitle="item.achieved_date"
                    :side="idx % 2 === 1 ? 'right' : 'left'"
                  >
                    <div>
                      <div><span class="text-bold" v-if="item.cate.collect_part === 'score'">成绩：</span>{{item.score}}</div>
                      <div><span class="text-bold" v-if="item.cate.collect_part === 'level'">等级：</span>{{item.level}}</div>
                      <div><span class="text-bold">指导教师：</span>{{advisorNameList[idx].join('、')}}</div>
                      <template v-if="joinerNameList[idx].length !== 1">
                        <div><span class="text-bold">参赛队员：</span>{{joinerNameList[idx].join('、')}}</div>
                      </template>
                      <q-btn color="primary" class="q-mt-md" @click="this.$router.push('/detail/' + item.pk)">查看详情</q-btn>
                    </div>
                  </q-timeline-entry>
                </template>

              </q-timeline>
            </div>
          </q-tab-panel>

          <q-tab-panel class="row" name="info-manage">
            <q-btn class="col-12" color="negative" size="lg" @click="doLogout()">退出登陆</q-btn>
          </q-tab-panel>

        </q-tab-panels>
      </q-card>
    </div>
  </div>
  </q-page>
</template>

<script>
import { useQuasar } from 'quasar'
import { ref, computed } from 'vue'
import { mapGetters } from 'vuex'

export default {
  data () {
    const joinerNameList = []
    const advisorNameList = []
    const getMyList = ref([])
    const $q = useQuasar()
    return {
      tab: ref('comps'),
      layout: computed(() => {
        return $q.screen.lt.sm ? 'dense' : ($q.screen.lt.md ? 'comfortable' : 'loose')
      }),
      joinerNameList,
      advisorNameList,
      getMyList
    }
  },
  async mounted () {
    this.$store.dispatch('comp/getMyList')
      .then((list) => {
        this.getMyList = list
        for (const t of list) {
          this.joinerNameList.push(t.join_list.filter(o => o.restrict_type === 'joiner').map(o => o.name))
          this.advisorNameList.push(t.join_list.filter(o => o.restrict_type === 'advisor').map(o => o.name))
        }
      })
  },
  computed: {
    ...mapGetters('session', [
      'isLogined',
      'getUser'
    ])
  },
  watch: {
    isLogined: function () {
      this.logoutGetOut()
    }
  },
  methods: {
    doLogout () {
      this.$store.dispatch('session/logout')
    },
    logoutGetOut () {
      if (!this.isLogined) {
        this.$router.replace('/')
      }
    }
  }
}
</script>
